package com.actor.testapplication.activity;

import android.os.Bundle;
import android.view.View;

import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.actor.testapplication.databinding.ActivityTinkerTestBinding;
import com.actor.tinker.TinkerConfig;
import com.blankj.utilcode.util.PathUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.model.HttpMethod;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;

import java.io.File;

/**
 * description: Tinker测试
 * company    :
 * @author    : ldf
 * date       : 2024/10/11 on 16:59
 */
public class TinkerTestActivity extends BaseActivity<ActivityTinkerTestBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Tinker patch热更新测试");
        TinkerConfig.setTinkerEnableWithSharedPreferences(this, true);

        viewBinding.btnToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showInfo();
            }
        });

        viewBinding.btnIsTinkerLoaded.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 检查是否安装了增量包
                boolean isTinkerLoaded = Tinker.with(mActivity).isTinkerLoaded();
                ToasterUtils.infoFormat("isTinkerLoaded: %b", isTinkerLoaded);
            }
        });


        //下载安装增量包
        String baseUrl = "http://192.168.3.25:801/D%3A/workspace_android/DevelopHelper/app/build/outputs/apk/tinkerPatch";
        String url = baseUrl + "/debug/app-debug-patch_signed_7zip.apk";
//        String url = baseUrl + "/release/app-release-patch_signed_7zip.apk";
        viewBinding.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyHttp.download(ApplicationLifecycle.getInstance())
                        .method(HttpMethod.GET)
                        .file(new File(PathUtils.getInternalAppFilesPath(), "app-debug-patch_signed_7zip.apk"))
//                    .file(new File(PathUtils.getInternalAppFilesPath(), "app-release-patch_signed_7zip.apk"))
                        .url(url)
                        .listener(new OnDownloadListener() {
                            @Override
                            public void onDownloadProgressChange(File file, int progress) {
                            }
                            @Override
                            public void onDownloadSuccess(File file) {
                                ToasterUtils.show("补丁下载成功!");
                            }
                            @Override
                            public void onDownloadFail(File file, Throwable throwable) {
                                ToasterUtils.error("补丁下载失败!");
                            }
                        }).start();
            }
        });

        viewBinding.loadPatch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(PathUtils.getInternalAppFilesPath(), "app-debug-patch_signed_7zip.apk");
//                File file = new File(PathUtils.getInternalAppFilesPath(), "app-release-patch_signed_7zip.apk");
                if (file.exists()) {
                    TinkerInstaller.onReceiveUpgradePatch(mActivity, file.getAbsolutePath());
                } else {
                    ToasterUtils.warning("请先下载补丁!");
                }
            }
        });

        viewBinding.cleanPatch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清除增量包
                Tinker.with(mActivity).cleanPatch();
            }
        });

        viewBinding.killSelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareTinkerInternals.killAllOtherProcess(getApplicationContext());
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }


    public void showInfo() {
        if (true) {
            float x = 1/2f;
            ToasterUtils.errorFormat("司法考试电缆敷设代理费立卡: %f", x);
        } else {
            ToasterUtils.error("2222222222222222222...");
//            startActivity(TinkerActivity0.class);
        }

        // add more Build Info
//        final StringBuilder sb = new StringBuilder();
//        Tinker tinker = Tinker.with(getApplicationContext());
//        if (tinker.isTinkerLoaded()) {
//            sb.append(String.format("[patch is loaded] \n"));
//            sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildConfig.TINKER_ID));
//            sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));
//
//            sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
//            sb.append(String.format("[TINKER_ID] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName(ShareConstants.TINKER_ID)));
//            sb.append(String.format("[packageConfig patchMessage] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName("patchMessage")));
//            sb.append(String.format("[TINKER_ID Rom Space] %d k \n", tinker.getTinkerRomSpace()));
//
//        } else {
//            sb.append(String.format("[patch is not loaded] \n"));
//            sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildInfo.TINKER_ID));
//            sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));
//
//            sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
//            sb.append(String.format("[TINKER_ID] %s \n", ShareTinkerInternals.getManifestTinkerID(getApplicationContext())));
//        }
//        sb.append(String.format("[BaseBuildInfo Message] %s \n", BaseBuildInfo.TEST_MESSAGE));
//
//        viewBinding.textView.setText(sb);
    }
}